#include <sourcemod>
#include <sdktools>
#pragma semicolon 1
#define PLUGIN_VERSION "1.01"
 
new bool:punish [MAXPLAYERS+1];
new bool:healthok [MAXPLAYERS+1];



public Plugin:myinfo =
{
	name = "帮助系统",
	author = "小海",
	description = "",
	version = PLUGIN_VERSION,
	url = "http://www.sourcemod.net"
};

new Handle:h_cvarPrintType=INVALID_HANDLE;
  
new l4d_help_revive_bonus;
new l4d_help_heal_bonus;

new l4d_help_heal_punish;
new l4d_help_heal_punish_less;

new h_cvarhelpprint;
new h_cvarfriendlyfireprint;
  

public OnPluginStart()
{
	//create version convar
	CreateConVar("l4d_help_version", PLUGIN_VERSION, "", FCVAR_REPLICATED|FCVAR_NOTIFY);
	

   
 	h_cvarPrintType = CreateConVar("l4d_help_whiteblack_print_type", "3", "黑白状态显示 0: 禁用 , 1 聊天栏, 2 中间 ， 3全部.", FCVAR_PLUGIN);
 
 	h_cvarhelpprint = CreateConVar("l4d_help_print", "1", "是否启动帮助显示", FCVAR_PLUGIN);
 	h_cvarfriendlyfireprint = CreateConVar("l4d_help_friedfire_print", "1", "是否启动队友伤害显示显示", FCVAR_PLUGIN);

	l4d_help_revive_bonus=CreateConVar("l4d_help_revive_bonus", "15", " 拉人奖励", FCVAR_PLUGIN);
	l4d_help_heal_bonus=CreateConVar("l4d_help_heal_bonus", "30", "治疗奖励 ", FCVAR_PLUGIN);

	l4d_help_heal_punish=CreateConVar("l4d_help_heal_punish", "3", "血多使用包惩罚  0： 不惩罚， 1， 惩罚打包者：2：惩罚被打包者， 3：都惩罚", FCVAR_PLUGIN);
	l4d_help_heal_punish_less=CreateConVar("l4d_help_heal_punish_less", "50", "惩罚血量界限 ", FCVAR_PLUGIN);

	new h_GameMode = FindConVar("mp_gamemode");
	decl String:GameName[16];
	GetConVarString(h_GameMode, GameName, sizeof(GameName));
	new GameMode;
	if (StrEqual(GameName, "survival", false))
		GameMode = 3;
	else if (StrEqual(GameName, "versus", false) || StrEqual(GameName, "teamversus", false) || StrEqual(GameName, "scavenge", false) || StrEqual(GameName, "teamscavenge", false))
		GameMode = 2;
	else if (StrEqual(GameName, "coop", false) || StrEqual(GameName, "realism", false))
		GameMode = 1;
	else
	{
		GameMode = 0;
 	}
	if(GameMode==2)return;


 	HookEvent("revive_success", EventReviveSuccess);
 	HookEvent("heal_success", EventHealSuccess);
 	HookEvent("heal_begin", EventHealBegin);
 	HookEvent("friendly_fire", EventFriendlyFire);
 	HookEvent("pills_used", Event_PillUsed);
	HookEvent("round_start", RoundStart);
	HookEvent("boomer_near", boomer_near);
 
	AutoExecConfig(true, "l4d_help_new_v10");
 	reset();
 
	RegConsoleCmd("sm_sea", Command_sea);
	 
}
public Action:Command_sea(client, args)
{
 	PrintToChatAll("帮助系统 小海");
	return Plugin_Handled;
}
  
public Action:RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	reset();
	return Plugin_Continue;
}
reset()
{
	for (new x = 0; x < MAXPLAYERS+1; x++)
	{
 			punish[x]=false;
			healthok[x]=false;
	}	 
 
}
public EventFriendlyFire(Handle:event, const String:name[], bool:dontBroadcast)
{
	if(GetConVarInt(h_cvarfriendlyfireprint)==0)return;
	new victim = GetClientOfUserId(GetEventInt(event, "victim"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new guilty = GetClientOfUserId(GetEventInt(event, "guilty"));
	new type = GetClientOfUserId(GetEventInt(event, "type"));
 	
	if( GetClientTeam(victim)==2)
	{
 		decl String:attackName[64];
 		decl String:victimName[64];
 		decl String:guiltyName[64];

		GetClientName(attacker, attackName, sizeof(attackName));
		GetClientName(victim, victimName, sizeof(victimName));
		GetClientName(guilty, guiltyName, sizeof(guiltyName));
		
		if(attacker==victim)
		{
			PrintToChatAll("\x01[sea]\x04 %s \x03喜欢\x04自残", attackName, victimName, guiltyName, type);
		}
		else
		{	
			if(attacker==guilty)
			{
				PrintToChatAll("\x01[sea]\x04 %s \x03伤害了\x04 %s", attackName, victimName);
			}
			else if(guilty==victim)
			{
				PrintToChatAll("\x01[sea]\x04 %s \x03伤害了\x04 %s,\x03可这不能怪他", attackName, victimName);
			}
			else
			{
				PrintToChatAll("\x01[sea]\x04 %s \x03伤害了\x04 %s,\x03起因是:\x04 %s", attackName, victimName, guiltyName);
			}
		}
	}
	return;
}
public boomer_near(Handle:event, const String:name[], bool:dontBroadcast)
{
 	new target = GetClientOfUserId(GetEventInt(event, "victim"));
	PrintHintText(target, "Boomer 接近了");

}
public EventReviveSuccess(Handle:event, const String:name[], bool:dontBroadcast)
{
	new player = GetClientOfUserId(GetEventInt(event, "userid"));
	new target = GetClientOfUserId(GetEventInt(event, "subject"));
	
	if(player==0 || target==0)return;

 	decl String:targetName[64];
 	decl String:palyerName[64];
 
	GetClientName(target, targetName, sizeof(targetName));
	GetClientName(player, palyerName, sizeof(palyerName));
	
	if(player!=target )
	{
		if(GetConVarInt(h_cvarhelpprint)==1) PrintToChatAll("\x01[sea]\x04 %s \x03救起了\x04 %s", palyerName, targetName);
		if(GetConVarInt(l4d_help_revive_bonus)>0 && !GetEventBool(event, "ledge_hang") )
		{
			PrintHintText(player, "帮助队友将会得到奖励");
			AddHealth(player,  GetConVarInt(l4d_help_revive_bonus) );
		}
	}

	if(GetEventBool(event, "lastlife"))
	{
  		
		new noticetype=GetConVarInt(h_cvarPrintType);
 		if(noticetype != 0)  
 		{
			for( new x = 1; x <= GetMaxClients(); x++)
			{
				if(!IsClientInGame(x) || GetClientTeam(target)!=2 || x == target || IsFakeClient(x)) 
					continue;
					
				if(noticetype == 2 || noticetype == 3) PrintHintText(x, "%s 进入黑白状态", targetName);
				if(noticetype == 1 || noticetype == 3) PrintToChat(x, "\x01[sea]\x04 %s \x03进入\x04黑白\x03状态", targetName);
			}
		}	
	}
	return;
}
 
public EventHealSuccess(Handle:event, const String:name[], bool:dontBroadcast)
{
	new player = GetClientOfUserId(GetEventInt(event, "userid"));
	new target = GetClientOfUserId(GetEventInt(event, "subject"));
	
	if(player==0 || target==0)return;

 
 	decl String:targetName[64];
 	decl String:palyerName[64];
 
	GetClientName(target, targetName, sizeof(targetName));
	GetClientName(player, palyerName, sizeof(palyerName));
	
 	if(player!=target )
	{
		if(healthok[target] && GetConVarInt(l4d_help_heal_punish)!=0)
		{
			if(GetConVarInt(l4d_help_heal_punish)==1  )
			{
				AddHealth(player,  -GetConVarInt(l4d_help_heal_bonus)*2);
			}
			else if(GetConVarInt(l4d_help_heal_punish)==2  )
			{
				punish[target]=true;
				CreateTimer(1.0, HurtPlayer, target, TIMER_REPEAT);
				PrintToChatAll("\x01[sea]\x04 %s \x03 给\x04 %s \x03打包, 致使他进入\x04病危\x03状态", palyerName, targetName);
				PrintHintText(player, "你的行为导致了 %s 的死亡", targetName);
				PrintHintText(target, "你已经命不久矣");
			}
			else if(GetConVarInt(l4d_help_heal_punish)==3)
			{
				AddHealth(player,  -GetConVarInt(l4d_help_heal_bonus)*2);
				punish[target]=true;
				CreateTimer(1.0, HurtPlayer, target, TIMER_REPEAT);
				PrintToChatAll("\x01[sea]\x04 %s \x03 给\x04 %s \x03打包, 致使他进入\x04病危\x03状态", palyerName, targetName);
				PrintHintText(player, "你的行为导致了 %s 的死亡", targetName);
				PrintHintText(target, "你已经命不久矣");

			}
			//ForcePlayerSuicide(target);
 		}
		else 
		{
			if(GetConVarInt(h_cvarhelpprint)==1) PrintToChatAll("\x01[sea]\x04 %s \x03治疗了\x04 %s", palyerName, targetName);
			AddHealth(player,  GetConVarInt(l4d_help_heal_bonus) );
			SetEntityHealth(target,  100 );
		}
	}
	else
	{
		if(healthok[target] && GetConVarInt(l4d_help_heal_punish)!=0)
		{
			PrintToChatAll("\x01[sea]\x04 %s \x03没事乱打包, 已经进入\x04病危\x03状态", targetName);
			PrintHintText(target, "你已经命不久矣");
			punish[target]=true;
			CreateTimer(1.0, HurtPlayer, target, TIMER_REPEAT);
			//ForcePlayerSuicide(target);
 		}
		else 
		{
			if(GetConVarInt(h_cvarhelpprint)==1) PrintToChatAll("\x01[sea]\x04 %s \x03治疗了\x04自己", palyerName, targetName);
 		}
	}

	return;
}

public Action:HurtPlayer(Handle:timer, any:client)
{
	new bool:quit=false;
 	if (client<=0) quit=true; 
	else
	{
		if (!IsClientInGame(client)) quit=true;
		else if (!IsPlayerAlive(client)) quit=true;
		if(!punish[client])quit=true;
	}
	if(quit)
	{
		punish[client]=false;
		return Plugin_Stop;
	}
	else
	{
		new propincapcounter = FindSendPropInfo("CTerrorPlayer", "m_currentReviveCount");
		new count = GetEntData(client, propincapcounter, 1);
		if(count==0)		DamageEffect(client, "5");
		else  DamageEffect(client, "15");
		return Plugin_Continue;
	}
 } 
public Action:HurtPlayer2(Handle:timer, any:client)
{
	new bool:quit=false;
 	if (client<=0) 	quit=true; 
	else
	{
		if (!IsClientInGame(client)) quit=true;
		if (!IsPlayerAlive(client)) quit=true;
		if(!punish[client])quit=true;
	}
	if(quit)
	{
		punish[client]=false;
		return Plugin_Stop;
	}
	else
	{
		new propincapcounter = FindSendPropInfo("CTerrorPlayer", "m_currentReviveCount");
		new count = GetEntData(client, propincapcounter, 1);
		if(count==0)		DamageEffect(client, "20");
		else  DamageEffect(client, "60");
		return Plugin_Continue;
	}
 } 
stock DamageEffect(target, String:demage[])
{
	new pointHurt = CreateEntityByName("point_hurt");			// Create point_hurt
	DispatchKeyValue(target, "targetname", "hurtme");			// mark target
	DispatchKeyValue(pointHurt, "Damage", demage);					// No Damage, just HUD display. Does stop Reviving though
	DispatchKeyValue(pointHurt, "DamageTarget", "hurtme");		// Target Assignment
	DispatchKeyValue(pointHurt, "DamageType", "65536");			// Type of damage
	DispatchSpawn(pointHurt);									// Spawn descriped point_hurt
	AcceptEntityInput(pointHurt, "Hurt"); 						// Trigger point_hurt execute
	AcceptEntityInput(pointHurt, "Kill"); 						// Remove point_hurt
	DispatchKeyValue(target, "targetname",	"cake");			// Clear target's mark
}
public Action:Event_PillUsed(Handle:hEvent, const String:name[], bool:dontBroadcast)
{
 	new target = GetClientOfUserId(GetEventInt(hEvent, "userid"));

	if(punish[target])
	{
		decl String:palyerName[64];
		GetClientName(target, palyerName, sizeof(palyerName));

		PrintToChatAll("\x01[sea]\x03生命垂危的\x04 %s \x03使用\x04药丸\x03, 加速了自己的死亡", palyerName);
		CreateTimer(1.0, HurtPlayer2, target, TIMER_REPEAT);
	}
   
 	return Plugin_Continue;
}
public EventHealBegin(Handle:event, const String:name[], bool:dontBroadcast)
{
	new player = GetClientOfUserId(GetEventInt(event, "userid"));
	new target = GetClientOfUserId(GetEventInt(event, "subject"));
	
	if(player==0 || target==0)return;

 
 	decl String:targetName[64];
 	decl String:palyerName[64];
 
	GetClientName(target, targetName, sizeof(targetName));
	GetClientName(player, palyerName, sizeof(palyerName));
	
	if(punish[target])
	{
		PrintToChatAll("\x01[sea]\x03生命垂危的\x04 %s \x03再次尝试\x04打包\x03, 加速了自己的死亡", targetName);
		CreateTimer(1.0, HurtPlayer2, target, TIMER_REPEAT);
	}

	else
	{
		new propincapcounter = FindSendPropInfo("CTerrorPlayer", "m_currentReviveCount");
 		new count = GetEntData(target, propincapcounter, 1);
		new hardhp = GetClientHealth(target) + 0; 
		new less=GetConVarInt(l4d_help_heal_punish_less);
		if(count==0 && hardhp>=less)
		{
			healthok[target]=true;
			if(player==target)
			{
				PrintHintText(player, "你还不需要治疗");
			}
			else 
			{
				PrintHintText(player, "%s 还不需要治疗", targetName);
				PrintHintText(target, "你还不需要治疗");
			}
		}
		else
		{
			healthok[target]=false;
			if(player!=target)PrintHintText(player, "帮助队友将会得到奖励");
		}
	}
	return;
} 
AddHealth(client, add)
{
	if(add<=0)
	{
		new String:arg1[10];
		Format(arg1, sizeof(arg1), "%i", -add);
		DamageEffect(client, arg1);
	}
	else
	{
		new hardhp = GetClientHealth(client) + 0; 
		SetEntityHealth(client, hardhp + add);
	}
	return;
}